package com.bear.fastrepair.base

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.bear.fastrepair.utils.ViewBindingHelper.inflateBindingWithGeneric

/**
 * @description
 * @author jacky
 * @date 2021/11/17-14:38
 */
abstract class BaseFragment<VB : ViewBinding> : Fragment() {
    private var _binding: VB? = null
    protected val binding: VB by lazy {
        this._binding ?: throw NullPointerException("view binding must not be null")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        this._binding = inflateBindingWithGeneric(inflater)
        return this.binding.root
    }

    @SuppressLint("ResourceAsColor")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        this._binding = null
    }

    protected abstract val title: String

    /**
     * 初始化方法，如控件，ViewModel等
     */
    protected abstract fun init()
}